extends Node

func _process(_delta) -> void:
	pass
 
func start_all_timers() -> void:
	$ScoreTimer.start()
	$ObstacleSpawnTimer.start()
	$BackgroundTimer.start()
	$HintDisappearTimer1.start()

func stop_all_timers() -> void:
	$ScoreTimer.stop()
	$ObstacleSpawnTimer.stop()
	$BackgroundTimer.stop()
	$DarkerScreenTimer.stop()
	$DayNightSwitchTimer.stop() 
	
func on_scored() -> void:
	Global.G_score += Global.G_natural_score_increase
	$"/root/Game/UILayer/GamingUI/Score".text = "Score " + str(Global.G_score)

func _on_darker_screen_timer_timeout() -> void:
	if $"/root/Game/GlobalLightLevel".color.a <= 0.95:
		$"/root/Game/GlobalLightLevel".color.a += 0.015
	else:
		$"/root/Game".end()
		
func randomize_trail_color() -> Color:
	if Global.G_trail_color.is_empty():
		Global.G_trail_color = Global._trail_color.duplicate(true)
		get_node("%Achievement").reset("Rainbow")
		get_node("%Achievement").achieve("Rainbow")

	Global.G_trail_color.shuffle()
	return Global.G_trail_color.pop_back()

func switch_day_and_night() -> void:
	if Global.G_phase == Global.Phase.Day:
		Global.G_phase = Global.Phase.Night
		$"/root/Game/BackgroundLayer".fade_switch(Preload.Background_Night_1)
		$"/root/Game/Mechanics/DarkerScreenTimer".start()
		$"/root/Game/Music/DayMusic".stop()
		$"/root/Game/Music/NightMusic".play()
		
		$"/root/Game/Bird/BeamCooldown".stop()
		$"/root/Game/Bird".BeamReady = true
		$"/root/Game/ParticleFactory".CurrentBeamType = $"/root/Game/ParticleFactory".BeamType.Light
		$"/root/Game/UILayer/GamingUI/Beam".texture = Preload.LightBeamSkill
		$"/root/Game/UILayer/GamingUI/NightHint".show()
		$"/root/Game/Mechanics/HintDisappearTimer3".start()
		
		if get_node("%Achievement").is_achieved("Ending1"):
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.8
			Global.G_natural_score_increase = 25
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1
			$"/root/Game/Mechanics/DayNightSwitchTimer".start(50)
		else:
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.5
			Global.G_natural_score_increase = 20
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.3
	else:
		Global.G_phase = Global.Phase.Day
		$"/root/Game/Mechanics/DarkerScreenTimer".stop()
		$"/root/Game/Music/NightMusic".stop()
		$"/root/Game/Music/DayMusic".play()
		
		while $"/root/Game/GlobalLightLevel".color.a > 0.01:
			$"/root/Game/GlobalLightLevel".color.a -= 0.01
			await get_tree().create_timer(0.01).timeout
		if get_node("%Achievement").is_achieved("Ending1"):
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.3
			Global.G_natural_score_increase = 10
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.2
			$"/root/Game/Mechanics/DayNightSwitchTimer".start(30)
		else:
			Global.G_scrollSpeed = Global.G_baseScrollSpeed
			Global.G_natural_score_increase = 10
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.5
			
		
func _on_diffraction_hint_timer_time_out():
	$"/root/Game/UILayer/GamingUI/DiffractionHint".hide()
	
func _on_hint_disappear_timer_3_timeout():
	$"/root/Game/UILayer/GamingUI/NightHint".hide()
